﻿using MediatR;
using SlaughterItem.Common.Domains.Detection;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02._IPQC.ShowIPQC_
{
    /// <summary>
    /// 显示检测标准表数据
    /// </summary>
    public class ShowIPQCHandler : IRequestHandler<ShowIPQCDataCommand, Result>
    {
        public ShowIPQCHandler(IBaseRepository<IPQCData> iPQCDataRepository)
        {
            IPQCDataRepository = iPQCDataRepository;
        }

        public IBaseRepository<IPQCData> IPQCDataRepository { get; set; }
        public async Task<Result> Handle(ShowIPQCDataCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            var list = IPQCDataRepository.GetAll().Where(x => x.IsDeleteIPQC == false).ToList();
            if (list != null)
            {
                if (request.LiveAnimalsDocumentTIme != "")
                {
                    var IPQCDataTime = DateTime.Parse(request.LiveAnimalsDocumentTIme);
                    list = list.Where(x => x.LiveAnimalsDocumentTIme == IPQCDataTime).ToList();
                }
                if (!string.IsNullOrEmpty(request.IPQCType))
                {
                    list = list.Where(x => x.IPQCType == request.IPQCType).ToList();
                }
                if (request.IsIPQC == 1)
                {
                    list = list.Where(x => x.IsIPQC == true).ToList();
                }
                if (request.IsIPQC == 2)
                {
                    list = list.Where(x => x.IsIPQC == false).ToList();
                }
                if (!string.IsNullOrEmpty(request.IPQCName))
                {
                    list = list.Where(x => x.IPQCName == request.IPQCName).ToList();
                }
                result.Data = list;
                result.Code = (int)CommonErrorCode.Success;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
